/*
 *  captureCameraManager.cpp
 *  opencvExample
 *
 *  Created by 真鍋 大度 on 11/02/28.
 *  Copyright 2011 Rhizomatiks. All rights reserved.
 *
 */

#include "captureCameraManager.h"

void captureCameraManager::setup(int _widthCam, int _heightCam, int _widthSpiral, int _heightSpiral){
	widthCam = _widthCam;
	heightCam = _heightCam;
	widthSpiral	= _widthSpiral;
	heightSpiral = _heightSpiral;
	
#ifdef _USE_LIVE_VIDEO
	vidGrabber.setVerbose(true);
	vidGrabber.initGrabber(widthCam,heightCam);
#else
	vidPlayer.loadMovie("fingers.mov");
	vidPlayer.play();
#endif
	
    colorImg.allocate(widthCam,heightCam);
	grayImage.allocate(widthCam,heightCam);
	grayBg.allocate(widthCam,heightCam);
	grayDiff.allocate(widthCam,heightCam);
	
	bLearnBakground = true;
	threshold = 80;
	
	widthSpiral = _widthSpiral;
	heightSpiral = _heightSpiral;
	// small

	
	heightCamSmall = heightSpiral; 
	widthCamSmall = heightSpiral / 3.0f * 4.0f;
	
	cout << "heightCamSmall" << endl;
	cout << heightCamSmall << endl;
	cout << "widthCamSmall" << endl;
	cout << widthCamSmall << endl;

	graySpiralImage.allocate(widthSpiral, heightSpiral);
	graySmallImage.allocate(widthCamSmall, heightCamSmall);	
	pixelsGraySpiral = new unsigned char[widthSpiral * heightSpiral];	
	pixelsGrayCamSmall = new unsigned char[widthCamSmall * heightCamSmall];
	
}
void captureCameraManager::update(){
    bool bNewFrame = false;
	
#ifdef _USE_LIVE_VIDEO
	vidGrabber.grabFrame();
	bNewFrame = vidGrabber.isFrameNew();
#else
	vidPlayer.idleMovie();
	bNewFrame = vidPlayer.isFrameNew();
#endif
	
	if (bNewFrame){
		
#ifdef _USE_LIVE_VIDEO
		colorImg.setFromPixels(vidGrabber.getPixels(), widthCam, heightCam);
#else
		colorImg.setFromPixels(vidPlayer.getPixels(), widthCam, heightCam);
#endif
		
        grayImage = colorImg;
		
		graySmallImage.scaleIntoMe(grayImage, 1);
		pixelsGrayCamSmall = graySmallImage.getPixels();
		
		int j = 0;
		//スパイラルのマトリクスをセット
		for (int i=0; i<widthSpiral * heightSpiral; i++) {
			int px = i % widthSpiral;
			int py = i / widthSpiral;
			
			int offSetx = (widthSpiral - widthCamSmall) / 2;
			//中心のピクセルをセッティング
//
//			int offSetx = 0;
			if (px >= offSetx && px < offSetx + widthCamSmall) {
				int camSmallX = px - offSetx;
				int camSmallY = py;
				int k = camSmallX + widthCamSmall * camSmallY;
				pixelsGraySpiral[i] = pixelsGrayCamSmall[k];
			}
			//中心以外のピクセル 0~offset-1,  offset+ camWidth ~ w-1
			else {
				pixelsGraySpiral[i] = 0;
			}
		}
		
//		for (int i=0; i<widthCamSmall * heightCamSmall; i++) {
//			int px = i % widthCamSmall;
//			int py = i / heightCamSmall;
//			pixelsGraySpiral[i]
////			pixelsGraySpiral[i]
//		}
		
		graySpiralImage.setFromPixels(pixelsGraySpiral, widthSpiral, heightSpiral);

		
		
		if (bLearnBakground == true){
			grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)
			bLearnBakground = false;
		}
		
		// take the abs value of the difference between background and incoming and then threshold:
		grayDiff.absDiff(grayBg, grayImage);
		grayDiff.threshold(threshold);
		
		// find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
		// also, find holes is set to true so we will get interior contours as well....
		contourFinder.findContours(grayDiff, 20, (widthCam * heightCam)/3, 10, true);	// find holes
	}
	
	
}

unsigned char * captureCameraManager::getGrayPixels(){
	return pixelsGraySpiral;
}

void captureCameraManager::draw(){
	// draw the incoming, the grayscale, the bg and the thresholded difference
	ofSetHexColor(0xffffff);
	colorImg.draw(0,0);
	grayImage.draw(widthCam,0);
//	grayBg.draw(0,heightCam);
//	grayDiff.draw(widthCam,heightCam);
	graySmallImage.draw(0, heightCam);
	graySpiralImage.draw(0, heightCam + heightCamSmall);
	// then draw the contours:
	
/*	
	ofFill();
	ofSetColor(0x333333);
	ofRect(360,540,320,240);
	ofSetColor(0xffffff);
	
	// we could draw the whole contour finder
	//contourFinder.draw(360,540);
	
	// or, instead we can draw each blob individually,
	// this is how to get access to them:
    for (int i = 0; i < contourFinder.nBlobs; i++){
        contourFinder.blobs[i].draw(360,540);
    }
	
	// finally, a report:
	
	ofSetColor(0xffffff);
	char reportStr[1024];
	sprintf(reportStr, "bg subtraction and blob detection\npress ' ' to capture bg\nthreshold %i (press: +/-)\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate());
	ofDrawBitmapString(reportStr, 20, 600);
*/	
}